package com.mes.schedule.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
/**
 * 成组工序任务
 * date: Jun 22, 2015 5:12:18 PM <br/>  
 * @author Administrator  
 * @version   
 * @since JDK 1.6
 */
public class SGroupOperationTask extends SOperationTask implements Serializable {
	protected List<SOperationTask> memberTasks;

	@Override
	public String getBatchNum() {
		if (this.batchNum != null)
			return this.batchNum;
		else {
			TreeSet sb = new TreeSet<String>();

			for (SOperationTask operationTask : this.memberTasks) {
				sb.add(operationTask.getBatchNum());
			}
			return sb.toString();
		}

	}

	@Override
	public Integer getPriority() {
		if (this.priority == null) {
			// 取最小的优先级（优先级数越小越优先）
			int minPriority = 1000;
			if (this.memberTasks != null && this.memberTasks.size() > 0) {
				for (SOperationTask memberSO : this.memberTasks) {
					if (memberSO.getPriority() < minPriority)
						minPriority = memberSO.getPriority();

				}
			}
			return minPriority;
		} else
			return this.priority;
	}

	@Override
	public Double getSlackFactor() {
		if (this.slackFactor == null) {
			// 取最小的松弛系数
			double minSlack = 10000;
			if (this.memberTasks != null && this.memberTasks.size() > 0) {
				for (SOperationTask memberSO : this.memberTasks) {
					if (memberSO.getSlackFactor() < minSlack)
						minSlack = memberSO.getSlackFactor();

				}
			}
			return minSlack;
		} else {
			return this.slackFactor;
		}

	}

	@Override
	public void setSlackFactor(Double slackFactor) {
		if (this.memberTasks != null && this.memberTasks.size() > 0) {
			for (SOperationTask memberSO : this.memberTasks) {
				memberSO.setSlackFactor(slackFactor);
			}
		}
		super.setSlackFactor(slackFactor);
	}

	@Override
	public void setPriority(Integer priority) {
		if (this.memberTasks != null && this.memberTasks.size() > 0) {
			for (SOperationTask memberSO : this.memberTasks) {
				memberSO.setPriority(priority);
			}
		}

		super.setPriority(priority);
	}

	public SGroupOperationTask() {
		this.memberTasks = new ArrayList<SOperationTask>();
	}

	public boolean isInGroup(SOperationTask so) {
		if (this.memberTasks != null && this.memberTasks.size() > 0) {
			for (SOperationTask memberTask : this.memberTasks) {
				if (so.equals(memberTask))
					return true;
			}
		}
		return false;
	}

	@Override
	public void setTaskScheduleType(Integer taskScheduleType) {
		//只让通过构造函数修改
		//不按类型修改
		// TODO Auto-generated method stub
//		super.setTaskScheduleType(taskScheduleType);
	}

	@Override
	public Set<SOperationLink> getPredLinkSet() {
		// 成组任务返回其子任务的链接信息
		//Set<SOperationLink> groupPredLinkSet = new HashSet<SOperationLink>();
		//by lqm 2013-10-24
		//允许成组任务也有自己的链接
		Set<SOperationLink> groupPredLinkSet = this.predLinkSet;		
		
		if (this.memberTasks != null && this.memberTasks.size() > 0) {
			for (SOperationTask memberSO : this.memberTasks) {
				groupPredLinkSet.addAll(memberSO.predLinkSet);
			}
			// 如果属于组内成员的链接则取消
			Set<SOperationLink> toBeDeletedLinkSet = new HashSet<SOperationLink>();
			for (SOperationLink link : groupPredLinkSet) {
				if (this.isInGroup(link.getPredOperationTask()))
					toBeDeletedLinkSet.add(link);
			}

			for (SOperationLink link : toBeDeletedLinkSet) {
				groupPredLinkSet.remove(link);
			}
			toBeDeletedLinkSet = null;

		}

		return groupPredLinkSet;

	}

	@Override
	public Set<SOperationLink> getSuccLinkSet() {
		//Set<SOperationLink> groupSuccLinkSet = new HashSet<SOperationLink>();
		
		//by lqm 2013-10-24
		//允许成组任务也有自己的链接
		Set<SOperationLink> groupSuccLinkSet = this.succLinkSet;	
		
		
		if (this.memberTasks != null && this.memberTasks.size() > 0) {
			for (SOperationTask memberSO : this.memberTasks) {
				groupSuccLinkSet.addAll(memberSO.succLinkSet);
			}
			// 如果属于组内成员的链接则取消
			Set<SOperationLink> toBeDeletedLinkSet = new HashSet<SOperationLink>();
			for (SOperationLink link : groupSuccLinkSet) {
				if (this.isInGroup(link.getSuccOperationTask()))
					toBeDeletedLinkSet.add(link);
			}

			for (SOperationLink link : toBeDeletedLinkSet) {
				groupSuccLinkSet.remove(link);
			}
		}
		return groupSuccLinkSet;
	}

	@Override
	public void setPlanFinish(Date planFinish) {
		if (this.memberTasks != null && this.memberTasks.size() > 0) {
			for (SOperationTask memberSO : this.memberTasks) {
				memberSO.setPlanFinish(planFinish);
			}
		}
		super.setPlanFinish(planFinish);
	}

	@Override
	public void setPlanStart(Date planStart) {
		if (this.memberTasks != null && this.memberTasks.size() > 0) {
			for (SOperationTask memberSO : this.memberTasks) {
				memberSO.setPlanStart(planStart);
			}
		}
		super.setPlanStart(planStart);
	}

	@Override
	public void setDevice(SDevice device) {
		if (this.memberTasks != null && this.memberTasks.size() > 0) {
			for (SOperationTask memberSO : this.memberTasks) {
				memberSO.setDevice(device);
			}
		}
		super.setDevice(device);
	}

	@Override
	public void setNetDepth(Integer netDepth) {

			if (this.memberTasks != null && this.memberTasks.size() > 0) {
				for (SOperationTask memberSO : this.memberTasks) {
					memberSO.setNetDepth(netDepth);
				}
			}

		super.setNetDepth(netDepth);
	}

	@Override
	public void setDeviceUid(String deviceUid) {
		if (this.memberTasks != null && this.memberTasks.size() > 0) {
			for (SOperationTask memberSO : this.memberTasks) {
				memberSO.setDeviceUid(deviceUid);
			}
		}
		super.setDeviceUid(deviceUid);
	}

	@Override
	public void setAssignState(Integer assignState) {
		if (this.memberTasks != null && this.memberTasks.size() > 0) {
			for (SOperationTask memberSO : this.memberTasks) {
				memberSO.setAssignState(assignState);
			}
		}
		super.setAssignState(assignState);
	}

	@Override
	public int getTaskDistance() {
		// 取最小的工序距离
		int maxD = 0;
		if (this.memberTasks != null && this.memberTasks.size() > 0) {
			for (SOperationTask memberSO : this.memberTasks) {
				if (memberSO.getTaskDistance() > maxD)
					maxD = memberSO.getTaskDistance();
			}
		}
		return maxD;
	}

	/**
	 * @return the memberTasks
	 */
	public List<SOperationTask> getMemberTasks() {
		return memberTasks;
	}

	/**
	 * @param memberTasks
	 *            the memberTasks to set
	 */
	public void setMemberTasks(List<SOperationTask> memberTasks) {
		this.memberTasks = memberTasks;
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		try {
			super.destroy();
			
			memberTasks.clear();
			memberTasks=null;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
